package com.fei.controller;

import com.fei.bean.MusicBean;
import com.fei.bean.UserBean;
import com.fei.service.FileService;
import com.fei.service.MusicService;
import com.fei.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    FileService fileService;
    @Autowired
    MusicService musicService;

    @RequestMapping("/")
    public String toLogin(){
        return "signin";
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "signup";
    }

//    验证手机号是否注册过
    @RequestMapping("/validPhone")
    @ResponseBody
    public Boolean validPhone(String phone){
        return userService.validPhoneService(phone);
    }

//    验证用户名是否注册过
    @RequestMapping("/validName")
    @ResponseBody
    public Boolean validName(String name){
        return userService.validNameService(name);
    }



    //登录
    @RequestMapping("/login")
    public String login(UserBean bean, Model model){
//        获取当前用户
        Subject subject = SecurityUtils.getSubject();
//        封装用户的登陆数据
        UsernamePasswordToken token = new UsernamePasswordToken(bean.getName(),bean.getPassword());

        try {
            subject.login(token);
//            这个页面的请求写在MusicController
            return "redirect:/user/index";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg","用户名错误");
            return "signin";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "signin";
        }

    }



//    注销
    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/";
    }

//    注册
    @RequestMapping("/insertUser")
    public String insertUser(UserBean userBean, MultipartFile file1) throws IOException {
        System.out.println(userBean);

        File path = fileService.ParentPath();
        String file1Suffix = fileService.FileSuffix(file1.getOriginalFilename());

        //头像路径规则 //data//User//用户名//1.(png/jpg)
        String filePath = path + "//data//UserData//"+userBean.getName();
        Boolean f1 = fileService.FileUpload(filePath,"1."+file1Suffix, file1);

        userBean.setImage("/UserData/"+userBean.getName()+"/1."+file1Suffix);
        userService.registerUserService(userBean);
        return "redirect:/";
    }



}
